package hm.edu.cs;


import org.json.JSONException;

import org.json.JSONObject;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Parcel;
import android.os.Parcelable;
import android.util.Log;
public class Book implements Parcelable {

	private String title;
	private String jahr;
	private String author;
	private String status;
	private String statusfarbe;
	private String isbn;
	public static String ID_KEY = "_id";
	
	public Book(JSONObject json)  throws JSONException
	{
		setAuthor(json);
		setJahr(json);
		setStatusFarbe(json);
		setStatus(json);
		setIsbn(json);
		this.title = json.getString(DataBaseHelper.KEY_TITLE);
	}
	
	private Book()
	{
		
	}
	
	private void setStatus(JSONObject json) throws JSONException
	{
		try
		{
			this.status = json.getString(DataBaseHelper.KEY_STATUS);
		}
		catch(JSONException e)
		{
			Log.e(null, "Fehlende Daten: "+ DataBaseHelper.KEY_STATUS);
		}
	}
	
	private void setStatusFarbe(JSONObject json) throws JSONException
	{
		try
		{
			this.statusfarbe = json.getString(DataBaseHelper.KEY_STATUS_COLOR);
		}
		catch(JSONException e)
		{
			Log.e(null, "Fehlende Daten: "+ DataBaseHelper.KEY_STATUS_COLOR);
		}
	}
	
	private void setJahr(JSONObject json) throws JSONException
	{
		try
		{
			this.jahr = json.getString(DataBaseHelper.KEY_YEAR);
		}
		catch(JSONException e)
		{
			Log.e(null, "Fehlende Daten: "+ DataBaseHelper.KEY_YEAR);
		}
	}
	
	private void setAuthor(JSONObject json) throws JSONException
	{
		try
		{
			this.author = json.getString(DataBaseHelper.KEY_AUTHOR);
		}
		catch(JSONException e)
		{
			Log.e(null, "Fehlende Daten: "+ DataBaseHelper.KEY_AUTHOR);
		}
	}
	
	private void setIsbn(JSONObject json) throws JSONException
	{
		try
		{
			this.isbn = json.getString(DataBaseHelper.KEY_ISBN);
		}
		catch(JSONException e)
		{
			Log.e(null, "Fehlende Daten: "+ DataBaseHelper.KEY_ISBN);
		}
	}
	
	
	public void setTitle(String title) 
	{
		this.title = title;
	}

	public String getTitle() {
		return title;
	}

	public void setJahr(String jahr) {
		this.jahr = jahr;
	}

	public String getJahr() {
		return jahr;
	}

	public void setAuthor(String author) {
		this.author = author;
	}

	public String getAuthor() {
		return author;
	}

	public void setStatus(String status) {
		this.status = status;
	}

	public String getStatus() {
		return status;
	}

	public void setStatusfarbe(String statusfarbe) {
		this.statusfarbe = statusfarbe;
	}

	public String getStatusfarbe() {
		return statusfarbe;
	}

	public void setIsbn(String isbn) {
		this.isbn = isbn;
	}

	public String getIsbn() {
		return isbn;
	}
	@Override
	public String toString() {
		return getTitle();
	}

	public void save(DataBaseHelper dbHelper) {
		//dbHelper.getDB().execSQL(DataBaseHelper.queryInsert,new String[] {title,author,isbn,status,jahr,statusfarbe});
		ContentValues values = new ContentValues();
		values.put(DataBaseHelper.KEY_TITLE, title);
		values.put(DataBaseHelper.KEY_AUTHOR, author);
		values.put(DataBaseHelper.KEY_ISBN, isbn);
		values.put(DataBaseHelper.KEY_STATUS, status);
		values.put(DataBaseHelper.KEY_YEAR, jahr);
		values.put(DataBaseHelper.KEY_STATUS_COLOR, statusfarbe);
		Log.e(null, "dbHelper: " + dbHelper);
		Log.e(null, "values" + values);
		dbHelper.getDB().insert(DataBaseHelper.TABLE_BOOKS, null, values);
	}
	
	public int delete(DataBaseHelper dbHelper)
	{
		return dbHelper.getDB().delete(DataBaseHelper.TABLE_BOOKS, DataBaseHelper.KEY_ISBN+"=?" , new String[] {this.getIsbn()});
	}
	
	public static int deleteAllFavorites(SQLiteDatabase db)
	{
		return db.delete(DataBaseHelper.TABLE_BOOKS,null,null);
	}
	
	public static Book[] laodFavorites(SQLiteDatabase db)
	{
		
		Cursor cursor = db.query(DataBaseHelper.TABLE_BOOKS, DataBaseHelper.getAllColNames(), "", null, null, null, null);
		Book[] books = new Book[cursor.getCount()];
		for(int i = 0; i < cursor.getCount(); i++)
			
		{
			cursor.moveToNext();
			Book book = new Book();
			for (String colName : cursor.getColumnNames()) 
			{
				if(colName.equals(DataBaseHelper.KEY_TITLE))
				{
					book.setTitle(cursor.getString(cursor.getColumnIndex(colName)));
				}
				else if(colName.equals(DataBaseHelper.KEY_ISBN))
				{
					book.setIsbn(cursor.getString(cursor.getColumnIndex(colName)));
				}
				else if(colName.equals(DataBaseHelper.KEY_AUTHOR))
				{
					book.setAuthor(cursor.getString(cursor.getColumnIndex(colName)));
				}
				else if(colName.equals(DataBaseHelper.KEY_STATUS))
				{
					book.setStatus(cursor.getString(cursor.getColumnIndex(colName)));
				}
				else if(colName.equals(DataBaseHelper.KEY_YEAR))
				{
					book.setJahr(cursor.getString(cursor.getColumnIndex(colName)));
				}
				else if(colName.equals(DataBaseHelper.KEY_STATUS_COLOR))
				{
					book.setStatusfarbe(cursor.getString(cursor.getColumnIndex(colName)));
				}
			} 
			books[i] = book;
		}
		cursor.close();
		return books;
	}

	@Override
	public int describeContents() {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public void writeToParcel(Parcel dest, int flags) 
	{
    	// do nothing
	}
}
